using System.Collections;
using UnityEngine;

public class Cam : MonoBehaviour
{
	public static Cam instance;

	public Camera cam;

	public Transform tf;

	public Rigidbody2D rb;

	public Transform TF_CAM;

	public float screenRatio;

	public Vector3 defaultCamPos;

	public float defaultCamSize;

	public float targetCamSize;

	[HideInInspector]
	public Vector3 targetCamPos;

	[HideInInspector]
	public bool fullview;

	private float camSize;

	private void Awake()
	{
		instance = this;
		defaultCamPos = tf.position;
	}

	public void Init()
	{
		StopAllCoroutines();
		Time.timeScale = 1f;
		Time.fixedDeltaTime = Time.timeScale * 0.02f;
		TF_CAM.localPosition = Vector2.zero;
		cam.orthographicSize = defaultCamSize;
		tf.position = new Vector3(0f, 7f, tf.position.z);
	}

	public void Shake(float duration, float mag)
	{
		StartCoroutine(IE_Shake(duration, mag));
	}

	private IEnumerator IE_Shake(float duration, float mag)
	{
		float exitTime = 0f;
		while (exitTime < duration)
		{
			TF_CAM.localPosition = new Vector2(Random.Range(-1f, 1f) * mag, Random.Range(-1f, 1f) * mag);
			exitTime += Time.deltaTime;
			yield return new WaitForFixedUpdate();
		}
		TF_CAM.localPosition = Vector2.zero;
	}

	public void ZoomInAndOutForCharacterDie(Transform TF_Target)
	{
		StartCoroutine(IE_ZoomInAndOutForCharacterDie(TF_Target));
	}

	private IEnumerator IE_ZoomInAndOutForCharacterDie(Transform TF_Target)
	{
		while (cam.orthographicSize > targetCamSize + 0.1f)
		{
			cam.orthographicSize += (targetCamSize - cam.orthographicSize) * (Time.fixedUnscaledDeltaTime * 3f);
			if (cam.orthographicSize < targetCamSize)
			{
				cam.orthographicSize = targetCamSize;
			}
			tf.position = new Vector3(TF_Target.position.x, TF_Target.position.y, defaultCamPos.z);
			yield return new WaitForSecondsRealtime(0.02f);
		}
		yield return new WaitForSecondsRealtime(1f);
		cam.orthographicSize = defaultCamSize;
		tf.position = new Vector3(tf.position.x, 7f, defaultCamPos.z);
	}

	private void FixedUpdate()
	{
		if (GM.instance.playerIsActive)
		{
			targetCamPos = new Vector2(Player.instance.rb.position.x, defaultCamPos.y);
			rb.position += ((Vector2)targetCamPos - rb.position) * 0.01f;
		}
	}
}
